var currentJb="bgbm";
$(document).ready(function () {
	  //填充左侧保管部门
	var bgbmJson = ajaxResult("get",false,ProjectName+"/sysmanage.getIdAndNameInJb.do",{'tableName':'jb_bgbm'});
	loadLeftList(".nav-stacked",bgbmJson.rows,'id','name');	
    createTable('#jb_bgbm',ProjectName+'/sysmanage.getKfbglb.do.do','id',bgbmColumns);
	//点击保管部门页签
	$(".nav-tabs li:eq(0)").click(function(){
		currentJb = "bgbm";
		createTable('#jb_bgbm',ProjectName+'/sysmanage.getKfbglb.do','id',bgbmColumns);
	});
	
	//点击库房页签
	$(".nav-tabs li:eq(1)").click(function(){
		//获取保管部门
		currentJb = "kfjb";		
		//获取保管部门rows中的第一个Id
		var bgbmId=bgbmJson.rows[0]["id"];		
		//填充弹出框保管部门下拉列表
		loadSelect("#form_kfjb select[name=bgbmmc]",bgbmJson.rows,'id','name');	
		//生成默认的列表
		createTableWithOutPage('#jb_kfjb','/antiques/sysmanage.getBmKfBgyInfo.do','id',kfbgyColumns,{"bgbmid":bgbmId});
		
	});
	//点击不同的标签所生成的列表
	$('#kfSelBm2 li').click(function(){
		var bgbmid=$(this).text();		
		if(bgbmid=='典藏部') bgbmid='diancangbu';
		if(bgbmid=='书画部') bgbmid='shuhuabu';
		if(bgbmid=='自然部') bgbmid='ziranbu';
		var params =new Object();
		params["bgbmid"] = bgbmid;
	 	$('#jb_kfjb').bootstrapTable('refresh',{query:params});
	})
	
				
	
	//加载藏品类别列表
	$("#selCplb_btn").click(function(){		
		//获取并藏品类别列表
		var cplbJson = ajaxResult("get",false,ProjectName+"/sysmanage.getCplbIdAndName.do",{'tableName':'jb_cplb'});
		loadCheckBoxList("#cplbList",cplbJson.rows,'id','name');
	});
	
	//加载保管员列表
	$("#selBgy_btn").click(function(){		
		var bgyJson = ajaxResult("get",false,ProjectName+"/sysmanage.getBgymc.do");
		loadCheckBoxList("#bgyList",bgyJson.rows,'bgyid','bgymc');
	});
	
	//确定选择的藏品类别
	$("#btn_add_cplb").click(function(){	
		var selCplbIds = new Array();
		var selCplbNames = '';
		$("#selectCplbModal input[type=checkbox]:checked").each(function(index,content){
			selCplbIds.push($(this).val());
			//selCplbNames += $(this).next().text() +"<br/>";
				
			selCplbNames += $(this).next().text() + "*";
		})
		$("input[name=cplbids]").val(selCplbIds);
		$("#cplbnames").html(selCplbNames);
		$('#selectCplbModal').modal('hide');
	});
	
	//确定选择的保管员
	$("#btn_add_bgy").click(function(){	
	
		var selBgyNames = '';
		$("#selectBgyModal input[type=checkbox]:checked").each(function(index,content){
			selBgyNames += $(this).next().text() + "*";
		})
	 
		$("#bgynames").html(selBgyNames);
		$('#selectBgyModal').modal('hide');
	});
	
	//打开库房增加窗口
	$("#openKfjb_btn").click(function(){
		$('#form_kfjb')[0].reset();
		$("#btn_add_kfjb").text("增加");
	});
	
//----------------------增加/修改保管部门--------------------------------------
	$("#btn_add_bgbm").click(function(){
		if(checkform("#form_bgbm")){
		 if($("#form_bgbm input[name=id]").val()==null || $("#form_bgbm input[name=id]").val()=='')//新增
		 {
			var params_bgbm =$('#form_bgbm').serializeObject();
			var bgbmmc =params_bgbm["name"];
			var bgcplb =$("#cplbnames").html();
			   var result="";
			   result = ajaxResult("post",false,ProjectName+"/sysmanage.addKfbglb.do",{"bgbmmc":bgbmmc,"bgcplb":bgcplb});
			   if(result["status"] == 'ok'){
				   alertFunc("增加保管部门信息成功！");		
				   $('#bgbmModal').modal('hide');
				   $('#jb_bgbm').bootstrapTable('refresh');
			   }else{
				   alertFunc(result["message"]);
			   }	
	  	  }
		 	else{//修改
		 		var params_bgbm =$('#form_bgbm').serializeObject();
				var bgbmmc =params_bgbm["name"];
				var bgcplb =$("#cplbnames").html();
				
				var sendParams = new Object();
				sendParams.id = params_bgbm["id"];
				sendParams.bgbmmc=params_bgbm["name"];
				sendParams.cpflid=params_bgbm["cplbids"];
				sendParams.cpflmc=bgcplb;

				
				   var result="";
				   result = ajaxResult("post",false,ProjectName+"/sysmanage.updateKfbglb.do",sendParams);
				   if(result["status"] == 'ok'){
					   alertFunc("增加保管部门信息成功！");		
					   $('#bgbmModal').modal('hide');
					   $('#jb_bgbm').bootstrapTable('refresh');
				   }else{
					   alertFunc(result["message"]);
				   }	
				   $("input[type='hidden']").val("");
			}
		}
	});
	
//---------------------------------增加/修改库房--------------------------------------------
	$("#btn_add_kfjb").click(function(){
		if(checkform("#form_kfjb")){
			var params_kfjb =$('#form_kfjb').serializeObject();
		 if($("#form_kfjb input[name=id]").val()==null || $("#form_kfjb input[name=id]").val()==''){//新增
			
			var kfmc =params_kfjb["name"];
			var bgbmmc = $("#form_kfjb").find("option:selected").text();
			var bgymc =$("#bgynames").html();
			var description =params_kfjb["description"];
			var sorted =params_kfjb["sorted"];

			
			   var result="";
			   result = ajaxResult("post",false,ProjectName+"/sysmanage.addKfwhTable.do",{"kfmc":kfmc,"bgbmmc":bgbmmc,"bgymc":bgymc,"kfms":description,"sorted":sorted});
			   if(result["status"] == 'ok'){
				   alertFunc("增加库房信息成功！");		
				   $('#kfjbModal').modal('hide');
				   $('#jb_kfjb').bootstrapTable('refresh');
			   }else{
				   alertFunc(result["message"]);
			   }	
	  	  }else{//修改

	  		var id =params_kfjb["id"];

	  		var kfmc =params_kfjb["name"];
			var bgbmmc = $("#form_kfjb").find("option:selected").text();
			var bgymc =$("#bgynames").html();
			var description =params_kfjb["description"];
			var sorted =params_kfjb["sorted"];

			
			   var result="";
			   result = ajaxResult("post",false,ProjectName+"/sysmanage.updateKfwhTable.do",{"kfmc":kfmc,"bgbmmc":bgbmmc,"bgymc":bgymc,"description":description,"sorted":sorted,"id":id});
			   if(result["status"] == 'ok'){
				   alertFunc("修改库房信息成功！");		
				   $('#kfjbModal').modal('hide');
				   $('#jb_kfjb').bootstrapTable('refresh');
			   }else{
				   alertFunc(result["message"]);
			   }	
	  		  
	  	  }
	  	 
		}
	});

})

//加载库房维护左侧的导航栏
function loadLeftList(dom,data,value,name){
	var result="";
	if(data!=null || data!=""){
		$.each(data,function(index,content){
			if(index==0){
				result +="<li class='active' role='presentation' value='"+content[value]+"'><a href='#tab_cplb' role='tab' data-toggle='tab' value='"+content[value]+"'>"+content[name]+"</a></li><br/>";
			}else{
				result +="<li role='presentation' value='"+content[value]+"'><a href='#tab_cplb' role='tab' data-toggle='tab' value='"+content[value]+"'>"+content[name]+"</a></li><br/>";
			}
			
		})
	}
	$(dom).html(result);
}
//加载保管部门中藏品类别复选框List
function loadCheckBoxList(dom,data,value,name){
	var result="";
	if(data!=null || data!=""){
		$.each(data,function(index,content){
			result +="<input type='checkbox' value='"+content[value]+"'><span>"+content[name]+"</span><br/>";
		})
	}
	$(dom).html(result);
}

//加载库房维护 保管员复选框
function loadBgyCheckBoxList(dom,data,bgymc){
	var result="";
	if(data!=null || data!=""){
		$.each(data,function(index,content){
			result +="<input type='checkbox'><span>"+content[bgymc]+"</span><br/>";
		})
	}
	$(dom).html(result);
}

//增加/修改内容提交
function add_update_tbl(tabname,alertInfo,url){
	var recordID="";
	var recordID = $('#form_'+tabname+' input[name=id]').val();
	if(recordID == undefined ||recordID == null || recordID== ""){
	   var params =$('#form_'+tabname).serializeObject();
	   //var bgbmmc=params.rows.id
	   //var para ={"id":bgbmmc}
	   params["tableName"] = "jb_"+tabname;
	   var result="";
	   result = ajaxResult("post",false,ProjectName+url,para);
	   if(result["status"] == 'ok'){
		   alertFunc("增加"+alertInfo+"信息成功！");		
		   $('#'+tabname+'Modal').modal('hide');
		   $('#jb_'+tabname).bootstrapTable('refresh');
	   }else{
		   alertFunc(result["message"]);
	   }	
	}
	else{
	   var updateID = $('#form_'+tabname+' input[name=id]').val();
	   var params =$('#form_'+tabname).serializeObject();
	   params["tableName"] = "jb_"+tabname;
	   var result="";
	   if(updateID == undefined ||updateID != null || updateID != ""){
		   result = ajaxResult("post",false,ProjectName+url,params);
		   if(result["status"] == 'ok'){
			   alertFunc("修改"+alertInfo+"信息成功！");		
			   $('#'+tabname+'Modal').modal('hide');
			   $('#jb_'+tabname).bootstrapTable('refresh');
		   }else{
			   alertFunc(result["message"]);
		   }
	   }
	}
}

//点击增加按钮，改变模态框中保存按钮的值
function modal_add(tabname){
	$('#form_'+tabname)[0].reset();
	$("#btn_add_"+tabname).text("增加");
}

//点击修改按钮，填充修改表单
function modal_modi(data){
	var tabname = currentJb;
	loadData("#form_"+tabname,data);	
	$("#btn_add_"+tabname).text("修改");
	$('#'+tabname+'Modal').modal('show');
}


//删除记录
function del_jb(id){		
	var params = new Object();
	params["id"] = id;
	//params["tableName"] = currentJb;	
	if(currentJb=="kfjb"){
		delRecord("#jb_"+currentJb,"/sysmanage.deleteKfwhTable.do",params);
	}
	else{
		delRecord("#jb_"+currentJb,"/sysmanage.deleteKfbglb.do",params);	
	}	

}

/* 库房保管员对应关系表表头开始  */
var kfbgyColumns=[{
	 field: 'check',
	 checkbox: true
    },{
	 field: 'kfid',
	 title: '库房id'
    },{
     field: 'kfmc',
	 title: '库房名称'
    },{
     field: 'bgymc',
   	 title: '保管员名称'
    },{
     field: 'operate',
     title: '操作',
     formatter : operateFormat
    }];

/* 保管部门藏品分类对应关系表表头开始  */
var bgbmColumns=[{
	 field: 'check',
	 checkbox: true
    },{
     field: 'bgbmmc',
	 title: '保管部门名称'
    },{
     field: 'cpflmc',
   	 title: '藏品分类名称'
    },{
     field: 'operate',
     title: '操作',
     formatter : operateFormat
    }];


//表格操作列
function operateFormat(value, row, index) {  	
  	return "<a onclick='modal_modi("+JSON.stringify(row)+")' href='javascript:void(0)'>修改</a>&nbsp;&nbsp;&nbsp;<a onclick='del_jb("+row.id+")' href='javascript:void(0)'>删除</a>";
} 
